package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.RefundRecordDao;
import cn.gson.crm.model.mapper.RefundRecordMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.*;
import cn.gson.crm.model.pojos.Customer;
import cn.gson.crm.model.pojos.MoneyBackPlan;
import cn.gson.crm.model.pojos.RefundRecord;
import cn.gson.crm.model.pojos.Shuliang;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class RefundRecordService {
    @Autowired
    RefundRecordMapper refundRecordMapper;
    @Autowired
    RefundRecordDao refundRecordDao;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //查询当前登录人所有的回款记录
    public Map<String,Object>  selectref(HttpSession session, Integer rid, Integer page, Integer size){
        rid=fouyuan(session);
        List<RefundRecord> refundRecordList2=refundRecordMapper.selectref(rid);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<RefundRecord> refundRecordList=refundRecordMapper.selectref(rid);
        //查询当前人下所有客户
        User login = (User)session.getAttribute("login");
        int ygId= (int) login.getEmployees().get(0).getYgId();
        List<Customer> customerList=refundRecordMapper.selectcustomer(ygId);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",refundRecordList);
        map.put("customerList",customerList);
        map.put("tong",refundRecordList2);
        return map;

    };
    //通过客户id查询所有回款计划未回
    public List<MoneyBackPlan> selecthui(Integer cid){
        return refundRecordMapper.selecthui(cid);
    };
    //回款多条件查询
    public Map<String,Object> selectwhere(HttpSession session,Integer rid, String where,Integer page, Integer size){
        rid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<RefundRecord> refundRecordList=refundRecordMapper.selectwhere(rid,where);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",refundRecordList);
        return map;
    };
    //时间查询
    public Map<String,Object> selecttime(HttpSession session,Integer rid, String statr,String endtime,Integer page, Integer size){
        rid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<RefundRecord> refundRecordList=refundRecordMapper.selecttime(rid,statr,endtime);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",refundRecordList);
        return map;
    };
    //新增
    public void  saveref(RefundRecord refundRecord){
        refundRecordDao.save(refundRecord);

        refundRecordMapper.updatehui((int) refundRecord.getMoneyBackPlan().getBackId());
        refundRecordMapper.updatehuiid((int) refundRecord.getRefundId(),(int) refundRecord.getMoneyBackPlan().getBackId());
        //查询计算回款总金额
        int zom=0;
        int omoney=0;
        List<RefundRecord> refundRecordList=refundRecordMapper.selectUU((int) refundRecord.getOrderByOrderId().getOrderId());
        for(int i=0;i<refundRecordList.size();i++){
            System.out.println(refundRecordList.get(0));
            zom+=refundRecordList.get(i).getRefundMoney();
            omoney= Math.toIntExact(refundRecordList.get(i).getOrderByOrderId().getOrderMoney());
        }
        System.out.println(zom+"--------------------------------------");
        System.out.println(omoney+"--------------------------------------");
        //修改订单回款金额
        zom= (int) (zom+refundRecord.getRefundMoney());
        System.out.println(zom+"--------------2222222------------------------");
        if(refundRecordList.size()>0){
            refundRecordMapper.updateordermoney((int) zom,(int) refundRecord.getOrderByOrderId().getOrderId());
            //判断是否回款金额等于订单如果是就修改订单支付状态
            if(zom==omoney){
                refundRecordMapper.updateorder2((int) refundRecord.getOrderByOrderId().getOrderId());
            }
        }

    }
    //根据回款计划id查询此记录
    public List<MoneyBackPlan> selectm(Integer mid){
       return refundRecordMapper.selectm(mid);
    };
    //统计回款记录
    public List<Shuliang> selectrefundRecord(){
        List<Shuliang> list=refundRecordMapper.selectrefundRecord();
        System.out.println(list);
        return list;
    }
}
